package com.qq.xrx1002.androidgame;

import android.view.MotionEvent;

import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.instant.CCHide;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCDelayTime;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.transitions.CCJumpZoomTransition;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by Alyson on 2018/11/6.
 */

class LogoLayer extends CCLayer {
    public LogoLayer() {
        logo1();
    }

    private void logo1() {
        CCSprite ccSprite_logo1=CCSprite.sprite("logo/logo1.png");
        ccSprite_logo1.setAnchorPoint(0,0);//不修改默认坐标，修改锚点，默认锚点在左下角为中点
        addChild(ccSprite_logo1);//把图层加入到子节点，默认为显示图层
        CCDelayTime ccDelayTime=CCDelayTime.action(2);//商标延时2秒
        CCHide ccHide=CCHide.action();//隐藏方法
        CCCallFunc ccCallFunc=CCCallFunc.action(this,"logo2");//调用logo2的方法
        //串联的方式把延时、隐藏方法、调用连起来运行
        CCSequence ccSequence=CCSequence.actions(ccDelayTime,ccHide,ccCallFunc);
        ccSprite_logo1.runAction(ccSequence);//运行这个图层
    }
    public void logo2() {//CCCallFunc回调的方法必须为public
        CCSprite ccSprite_logo2=CCSprite.sprite("logo/logo2.png");
        CGSize winSize= CCDirector.sharedDirector().winSize();//设置位置,不修改默认锚点，修改坐标，默认锚点为中点
        ccSprite_logo2.setPosition(winSize.getWidth()/2,winSize.getHeight()/2);
        addChild(ccSprite_logo2);//把图层加入到子节点，默认为显示图层
        CCDelayTime ccDelayTime=CCDelayTime.action(2);//商标延时2秒
        CCHide ccHide=CCHide.action();//隐藏方法
        CCCallFunc ccCallFunc=CCCallFunc.action(this,"cg");//调用cg的方法
        //串联的方式把延时、隐藏方法、调用连起来运行
        CCSequence ccSequence=CCSequence.actions(ccDelayTime,ccHide,ccCallFunc);
        ccSprite_logo2.runAction(ccSequence);//运行这个图层
    }
    public void cg() {//CCCallFunc回调的方法必须为public
        CCSprite ccSprite_cg=CCSprite.sprite("cg/cg00.png");
        ccSprite_cg.setAnchorPoint(0,0);
        addChild(ccSprite_cg);//把图层加入到子节点，默认为显示图层
        //帧序列，使用数组列表
        ArrayList<CCSpriteFrame> frames=new ArrayList<>();
        for (int i = 0; i <19 ; i++) {//总共19张图片
            CCSpriteFrame ccSpriteFrame=CCSprite.sprite(String.format(Locale.CHINA,//Locale.CHINA：使用的语言为中文
                    "cg/cg%02d.png",i)).displayedFrame();//2位的帧序列的使用
            frames.add(ccSpriteFrame);
        }
        CCAnimation ccAnimation=CCAnimation.animationWithFrames(frames,0.2f);//CCAnimation：帧动画；frames,0.2f：0.2秒执行一帧
        CCAnimate ccAnimate=CCAnimate.action(ccAnimation,false);//帧动画是否重复，false表示不重复。也可以不写
        CCCallFunc ccCallFunc=CCCallFunc.action(this,"setTouch");//写入一个调用方法，可以触摸来实现点击
        //串联的方式来运行调用ccAnimate、ccCallFunc
        CCSequence ccSequence=CCSequence.actions(ccAnimate,ccCallFunc);
        ccSprite_cg.runAction(ccSequence);//运行这个图层
    }

    public void setTouch(){
        setIsTouchEnabled(true);
    }

    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        //坐标系的转换：Android体系的转换为opengl体系
        CGPoint cgPoint=convertTouchToNodeSpace(event);
        CGRect cgRect=CGRect.make(390,30,490,60);
        if (CGRect.containsPoint(cgRect,cgPoint)){
            CCScene ccScene=CCScene.node();
            ccScene.addChild(new MenuLayer());//MenuLayer()菜单方法继承图层
            CCJumpZoomTransition ccJumpZoomTransition=CCJumpZoomTransition.transition(2,ccScene);//cocos跳跃的切换
            CCDirector.sharedDirector().runWithScene(ccJumpZoomTransition);//用导演来执行跳转切换
        }
        return super.ccTouchesBegan(event);
    }
}
